package com.service.impl;

import com.mapper.FlowMapper;
import com.mapper.GroupOrderMapper;
import com.pojo.GroupList;
import com.pojo.GroupOrder;
import com.service.GroupListService;
import com.service.GroupPayService;
import com.util.ServerResponse;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/4/16
 * Time: 12:03
 * Description: No Description
 */
public class GroupPayServiceImp implements GroupPayService {
    @Autowired
    private GroupOrderMapper groupOrderMapper;
    @Autowired
    private GroupListService groupListService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private Sid sid;
    @Override
    public ServerResponse saveOrder(GroupOrder groupOrder) {
        //查询团购商品信息
        ServerResponse groupInfsr = groupListService.getById(groupOrder.getGroupId());
        //判断团购商品是否下架
        if (groupInfsr.getStatus()!=1){
            return ServerResponse.getDataFailed("商品已经下架");
        }
        //查询团购商品信息
        GroupList groupInf =(GroupList)groupInfsr.getData();
        //查询团购商品剩余数量
        int remaining  = groupInf.getGroupAll() - groupInf.getGroupCount();
        System.out.println("商品总量"+remaining);
        System.out.println("已售数量"+groupInf.getGroupCount());
        //判断团购商品是否售完
        if (remaining<=0){
            return ServerResponse.getDataFailed("商品已售完");
        }
        //设置订单状态 2为待支付
        Byte status =2;
        groupOrder.setStatus(status);
        Calendar nowTime = Calendar.getInstance();
        //设置订单下单时间
        Date orderTime = nowTime.getTime();
        //设置订单超时时间
        nowTime.add(Calendar.MINUTE,15);
        Date overTime = nowTime.getTime();
        //设置已售数量加一
        groupInf.setGroupCount(groupInf.getGroupCount()+1);
        groupListService.modify(groupInf);
        //生成订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String result = sdf.format(orderTime);
        result+=groupOrder.getUserId();
        Long orderNum = Long.valueOf(result);
        System.out.println("订单号"+orderNum);
        //设置团购订单信息
        groupOrder.setUnitPrice(groupInf.getGroupPrice());
        if(null==groupOrder.getProdCount()){
            groupOrder.setProdCount(1);
        }
        System.out.println("购买数量"+groupOrder.getProdCount());
        System.out.println("单价"+groupOrder.getUnitPrice());
        groupOrder.setAllPrice(groupOrder.getProdCount()*groupOrder.getUnitPrice());
        System.out.println("总价"+groupOrder.getAllPrice());
        groupOrder.setOrderNum(orderNum);
        groupOrder.setOrderTime(orderTime);
        groupOrder.setOverTime(overTime);
        //执行groupOrderMapper添加方法
        int rows = groupOrderMapper.insert(groupOrder);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addDataSuccess(groupOrder);
        else
            return ServerResponse.addFailed();
    }

    @Override
    public void updateOrderStatus(Integer orderId, String alpayFlowNum, String paidAmount) {

    }

    @Override
    public GroupOrder getOrderById(Integer orderId) {
        return null;
    }
}
